﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using CashOrCard.Models;

namespace CashOrCard.Extensions
{
    public class CashOrCardRoleExtension : RoleProvider
    {
        public override string[] GetRolesForUser(string email)
        {            
           /* using (SignetContext objContext = new SignetContext())
            {
                var objUser = objContext.Users.FirstOrDefault(x => x.UserNumber == username);
                if (objUser != null)
                {
                    string[] roleStr = objUser.Roles.Select(x => x.Name).ToArray();
                    return roleStr;
                }
                else
                {
                    return null;
                }
            }*/
            if (HttpContext.Current.Cache[email + "permissions"] != null)
            {
                object cachedPermissions = HttpContext.Current.Cache[email + "permissions"];
                //List<string> cachedPermissionList = new List<string>();
                //cachedPermissionList = (List<string>)cachedPermissions;
                return (string[])cachedPermissions; //cachedPermissionList.ToArray();
            }
            else
            {
                using (CashOrCardContext objContext = new CashOrCardContext())
                {

                    var objUser = objContext.Users.FirstOrDefault(x => x.Email == email);
                    if (objUser != null)
                    {
                        List<string> roleStr = new List<string>();

                        foreach (string permission in objUser.Role.Permisstions.Select(x => x.Name))
                        {
                            roleStr.Add(permission);
                        }
                       
                        string[] roleArray = roleStr.Distinct().ToArray();
                        HttpContext.Current.Cache.Add(email + "permissions", roleArray, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0, 0, 0), System.Web.Caching.CacheItemPriority.Normal, null);
                        return roleArray;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}